<?php
include(WEIXIN_ROBOT_PLUGIN_DIR.'admin/admin-menus.php');
include(WEIXIN_ROBOT_PLUGIN_DIR.'admin/tables.php');

weixin_include_extends($adimn=true);	// 加载扩展

global $wpdb;

// $wpdb->weixin_messages		= WEIXIN_Message::get_table();
// $wpdb->weixin_subscribes	= WEIXIN_UserSubscribe::get_table();;
// $wpdb->weixin_users			= WEIXIN_User::get_table();

// 在插件页面添加快速设置链接
add_filter('plugin_action_links_' . plugin_basename(WEIXIN_ROBOT_PLUGIN_FILE), function( $links, $file ) {
	$links['setting']	= '<a href="'.admin_url('admin.php?page=weixin-robot').'">设置</a>';

	if(strpos(WEIXIN_ROBOT_PLUGIN_FILE, 'weixin-robot-test')){
		$links['info']	= '<span style="color:red; font-weight:bold;">测试版</span>';
	}else{
		$links['info']	= '<span style="color:green; font-weight:bold;">正式版</span>';
	}

	return array_reverse($links);
}, 10, 2 );


// 设置微信插件最后加载
add_filter('pre_update_option_active_plugins', function($active_plugins){
	$weixin_plugin	= plugin_basename(WEIXIN_ROBOT_PLUGIN_FILE);
	if(false !== ($plugin_key	= array_search($weixin_plugin, $active_plugins))){
		unset($active_plugins[$plugin_key]);
		$new_active_plugins = array();
		if($active_plugins){
			foreach ($active_plugins as $active_plugin) {
				$new_active_plugins[] = $active_plugin;
			}
		}

		$new_active_plugins[] = $weixin_plugin;

		return $new_active_plugins;
	}

	return $active_plugins;		
});

add_action('admin_head', function(){
	?>
	<script type="text/javascript">
	function show_wx_img(src, iframe_width=0, iframe_height=0, url='') {
		if(iframe_width){
			var img_html	= '<img id="img" src=\'' + src + '?' + Math.random() + '\' />';
		}else{
			var img_html	= '<img id="img" style="max-width:100%;" src=\'' + src + '?' + Math.random() + '\' />';
		}
		
		if(url){
			img_html	= '<a href="'+url+'" target="_blank">'+img_html+'</a>';
		}

		var frameid		= 'frameimg' + Math.random();

		if(iframe_width){
			window.iframe_html = '<body style="margin:0;padding:0;">'+img_html+'<script>window.onload = function() {wx_iframe=parent.document.getElementById(\'' + frameid + '\'); wx_img = document.getElementById(\'img\'); iframe_width=wx_iframe.width; iframe_height=wx_iframe.height; img_width = wx_img.width; img_height = wx_img.height; if((img_width/img_height)>(iframe_width/iframe_height)){ wx_img.style.height=\'100%\'; img_width=Math.ceil(iframe_height/img_height*img_width); wx_img.style.marginLeft=(iframe_width - img_width) / 2+\'px\'; }else{ wx_img.style.width=\'100%\'; img_height=Math.ceil(iframe_width/img_width*img_height); wx_img.style.marginTop=(iframe_height - img_height) / 2+\'px\'; } }<' + '/script></body>';

			return '<iframe id="' + frameid + '" src="javascript:parent.iframe_html;" width="'+iframe_width+'" height="'+iframe_height+'" frameBorder="0" scrolling="no"></iframe>';
			
			// document.write('<iframe id="' + frameid + '" src="javascript:parent.iframe_html;" width="'+iframe_width+'" height="'+iframe_height+'" frameBorder="0" scrolling="no"></iframe>');
		}else{
			window.iframe_html = '<body style="margin:0;padding:0;">'+img_html+'<script>window.onload = function() { parent.document.getElementById(\'' + frameid + '\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script></body>';

			return '<iframe id="' + frameid + '" src="javascript:parent.iframe_html;" width="100%" frameBorder="0" scrolling="no"></iframe>';
			// document.write('<iframe id="' + frameid + '" src="javascript:parent.iframe_html;" width="100%" frameBorder="0" scrolling="no"></iframe>');
		}
	}
	</script>
	<?php
});


add_filter('user_has_cap', function($allcaps, $caps, $args, $user){
	if(!empty($allcaps['manage_options'])){	// 管理员
		if($args[0] == 'view_weixin'){		// 设置管理员有 view_weixin 权限
			$allcaps[$args[0]]	= 1;
		}
	}

	return $allcaps;
}, 10, 4);

$today	= date('Y-m-d', current_time('timestamp'));

// 只保留三个月的消息
if(!wpjam_is_scheduled_event('weixin_delete_messages')) {
	$time	= strtotime(get_gmt_from_date($today.' 02:00:00')) + rand(0,7200);
	wp_schedule_event( $time, 'twicedaily', 'weixin_delete_messages' );
}


if (weixin_get_type() >= 3 && !wpjam_is_scheduled_event('weixin_get_mp_stats')) {
	$time	= strtotime(get_gmt_from_date($today.' 09:00:00')) + rand(10,720);	//每天9点左右获取昨天的数据	

	do_action('weixin_get_mp_stats','all');

    wp_schedule_event($time, 'daily', 'weixin_get_mp_stats', array('all'));
}


// if(!wpjam_is_scheduled_event('weixin_get_users')) {	
// 	$today	= date('Y-m-d', current_time('timestamp'));
// 	$time	= strtotime(get_gmt_from_date($today.' 03:00:00')) + rand(0,7200);

// 	wp_schedule_event( $time, 'daily', 'weixin_get_users' );
// }